package tools

import (
	"encoding/json"
	"github.com/sirupsen/logrus"
	"src/error_msg"
	"src/modle"
	"strings"
	"time"
)

const (
	//越南时间为utc+7
	Vietnam = 7
)

// 反序列化联系人json
func ParseContactsStr(jsonStr string) ([]string, error) {
	j := make([]modle.Contacts, 0)
	if e := json.Unmarshal([]byte(jsonStr), &j); e != nil {
		ne := error_msg.DBJsonStrError
		logrus.Error(e.Error(), ne.Error())
		return []string{}, ne
	}
	slice := make([]string, 0)
	for _, v := range j {
		strArr := StrTrimSpit(v.PhoneNum)
		slice = append(slice, strArr...)
	}
	return slice, nil
}

// 反序列化紧急联系人json
func ParseUrgencyContactsStr(jsonStr string) ([]string, error) {
	j := make([]modle.UrgencyContacts, 0)
	if e := json.Unmarshal([]byte(jsonStr), &j); e != nil {
		ne := error_msg.DBJsonStrError
		logrus.Error(e.Error(), ne.Error())
		return []string{}, ne
	}
	slice := make([]string, 0)
	for _, v := range j {
		strArr := StrTrimSpit(v.Phone)
		slice = append(slice, strArr...)
	}
	return slice, nil
}

// 字符串trim，分割，去重复
func StrTrimSpit(str string) []string {
	trimSpace := strings.ReplaceAll(str, " ", "")
	trim := strings.ReplaceAll(trimSpace, "-", "")
	spited := strings.Split(trim, ",")
	return spited
}

// 去重
func RemoveDuplicate(strArr []string) map[string]int {
	m := make(map[string]int)
	for _, v := range strArr {
		if v == "" {
			continue
		}
		m[v] = 1
	}
	return m
}

// 包含方法-1 没有其他有
func Includes(strArr []string, str string) int {
	for i := 0; i < len(strArr); i++ {
		if strArr[i] == str {
			return i
		}
	}
	return -1
}

// 获取越南当地时间
func GetVietnamTime() time.Time {
	return time.Now().UTC().Add(time.Hour * Vietnam)
}
